********************************************************************************
* Prepare sector and country lists
********************************************************************************
clear
set more off

*global rootfolder ""
global inputfolder "$rootfolder\data"
global outputfolder "$rootfolder\analysis\CS"
capture mkdir "$outputfolder"

********************************************************************************
global YEAR_START 1995
global YEAR_END 2011
********************************************************************************

********************************************************************************
* SECTORS
********************************************************************************

* FORMAT WIOD-NACE CONCORDANCE
clear
insheet using "$inputfolder\0_Concordances&OtherAuxiliaryFiles\Concordance_NACEr1.1-WIOD.txt", tab names clear
tostring  nace_r1_1, replace
gen zero="0"
gen dot="."
gen nace_2d=zero + nace_r1_1 if length(nace_r1_1)<2
replace nace_2d=nace_r1_1 if nace_2d==""
replace nace_2d=nace_2d + dot
keep wiod_code nace_2d
rename nace_2d nace_r1_1
replace wiod_code = strlower(wiod_code)
save "$outputfolder\tempconcordances.dta", replace

* all sectors alphabetically sorted:
clear
use "$outputfolder\tempconcordances.dta", clear
collapse (first) nace_r1_1, by(wiod_code)
drop nace_r1_1
encode(wiod_code), gen(ss)
gen s_alphabetic = _n
drop ss
sort s_alphabetic
save "$outputfolder\AllSectors.dta", replace
outsheet wiod_code using "$outputfolder\AllSectors.csv", comma replace


******************************************************************************************
* COUNTRIES
******************************************************************************************

* all countries:
clear
use "$inputfolder\0_Concordances&OtherAuxiliaryFiles\Name_Concordance.dta", clear
encode(wiod_name), gen(cc)
sort cc
gen c_alphabetic = _n
drop cc name nace_name oecdbtdi_name
order wiod_name c_alphabetic
save "$outputfolder\AllCountries.dta", replace
outsheet wiod_name using "$outputfolder\AllCountries.csv", comma replace

***********************************

* order c (countries)
use "$outputfolder\AllCountries.dta", clear
rename c_alphabetic c
save "$outputfolder\tempallc.dta", replace

* order s (sectors)
use "$outputfolder\AllSectors.dta", clear
rename s_alphabetic s
save "$outputfolder\tempalls.dta", replace

***********************************
* make T*n*m sets {c,s}_t for price indices
*******************
scalar y0_pi=1995
scalar yT_pi=2012
*******************

use "$outputfolder\AllCountries.dta", clear
scalar nnn=_N
use "$outputfolder\AllSectors.dta", clear
scalar mmm=_N

clear
set obs 1
expand 1+yT_pi-y0_pi
gen year=y0_pi+(_n-1)
expand 12
sort year
by year: gen month=_n
expand nnn
sort year month
by year month: gen c = _n
expand mmm
sort year month c
by year month c: gen s = _n
sort year month c s

joinby c using "$outputfolder\tempallc.dta"
joinby s using "$outputfolder\tempalls.dta"

save "$outputfolder\tempcsset.dta", replace

***********************************
* make T*n*m*n*m sets {c_I,s_I,c_E,s_E}_t for market/input/import shares
*******************
scalar y0_shares=1995
scalar yT_shares=2011
*******************

use "$outputfolder\AllCountries.dta", clear
scalar n=_N
use "$outputfolder\AllSectors.dta", clear
scalar m=_N

clear
set obs 1
expand 1+yT_shares-y0_shares
gen year=y0_shares+(_n-1)
expand n
sort year
by year: gen c_I = _n
expand m
sort year c_I
by year c_I: gen s_I = _n
expand n
sort year c_I s_I
by year c_I s_I: gen c_E = _n
expand m
sort year c_I s_I c_E
by year c_I s_I c_E: gen s_E = _n

rename c_I c
joinby c using "$outputfolder\tempallc.dta"
rename c c_I
rename wiod_name country
rename c_E c
joinby c using "$outputfolder\tempallc.dta"
rename c c_E
rename wiod_name partner
rename s_I s
joinby s using "$outputfolder\tempalls.dta"
rename s s_I
rename wiod_code country_ind
rename s_E s
joinby s using "$outputfolder\tempalls.dta"
rename s s_E
rename wiod_code partner_ind
compress
save "$outputfolder\tempcscsset.dta", replace
